package com.bac.rentmap.ad.flat.search;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Logger;

import org.springframework.beans.factory.annotation.Required;

import com.bac.rentmap.ad.flat.FlatAd;
import com.bac.rentmap.ad.flat.FlatAdDao;
import com.bac.rentmap.ad.flat.GeoLocation;
import com.bac.rentmap.ad.flat.LocalAddress;
import com.bac.rentmap.ad.flat.LocalAddressPrefix;
import com.bac.rentmap.distance.DistanceCalculator;
import com.bac.rentmap.distance.DistanceCheckPoint;
import com.bac.rentmap.distance.DistanceCheckPointProvider;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Text;

/**
 * Searches for {@link FlatAd} using datastore low level API.
 *
 * @author Andrew Osipenko
 */
public class DatastoreFlatAdSearcher implements FlatAdSearcher {
    private static final Logger log = Logger
            .getLogger(DatastoreFlatAdSearcher.class.getName());
    private FlatAdKeySearcher flatAdKeySearcher;

    private FlatAdDao flatAdDao;

    @Override
    public List<FlatAd> search(SearchParameters searchParameters) {
        List<Long> keys = flatAdKeySearcher.search(searchParameters);
        List<FlatAd> res = new ArrayList<FlatAd>(keys.size());
        for (Long key : keys) {
            FlatAd flatAd = flatAdDao.get(key);
            //if (flatAd != null)
            res.add(flatAd);
        }
        return res;
    }
    @Required
    public void setFlatAdKeySearcher(FlatAdKeySearcher flatAdKeySearcher) {
        this.flatAdKeySearcher = flatAdKeySearcher;
    }
    @Required
    public void setFlatAdDao(FlatAdDao flatAdDao) {
        this.flatAdDao = flatAdDao;
    }
}
